package com.woniuxy.service.impl;

import com.woniuxy.controller.info.MenuInfo;
import com.woniuxy.model.MenuModel;
import com.woniuxy.dao.MenuModelDao;
import com.woniuxy.service.MenuModelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.utils.CopyUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 马世亮
 * @since 2022-02-15
 */
@Service
public class MenuModelServiceImpl extends ServiceImpl<MenuModelDao, MenuModel> implements MenuModelService {

    @Resource
    private MenuModelDao menuModelDao;

    @Override
    public List<MenuInfo> findMenusByAccountId(Integer id) {
        List<MenuModel> menuModels = menuModelDao.selectMenusByAccountId(id);
        List<MenuInfo> menuInfos = CopyUtil.copyList(menuModels, MenuInfo.class);
        List<MenuInfo> result=new ArrayList<MenuInfo>();
        HashMap<Integer,MenuInfo> maps=new HashMap<Integer,MenuInfo>();

        for(MenuInfo mi: menuInfos){
            //childrenMenuInfo(mi);
            if(mi.getMenuParentId()==null){
                for(MenuInfo mm:menuInfos){
                    if (mm.getMenuParentId()!=null&&mm.getMenuParentId()==mi.getMenuId()) {
                        if (mi.getChildren()==null) {
                            mi.setChildren(new ArrayList<>());
                        }
                        List list=mi.getChildren();
                        list.add(mm);
                        mi.setChildren(list);
                    }
                }
                result.add(mi);
            }

        }


        return result;
    }
}
